<?php

require '../inc/init.php';
chkpurview('activity_manage');

$act = isset($_GET['act']) ? $_GET['act'] : NULL;
$act_id = isset($_POST['act_id'])?$_POST['act_id']:null;

if($act == 'save')		//保存新建活动
{
	$platformId		= isset($_POST['platform_id']) ? $_POST['platform_id'] : 0;	
	$severId			= isset($_POST['server_id']) ? $_POST['server_id'] : 0;
	
	$action 			= isset($_POST['action']) ? $_POST['action'] : 0;						//1表示不删除当前活动领取过的奖励,0为默认删除
	//活动名称
	$actName 		= isset($_POST['actName']) ? trim(strip_tags($_POST['actName'])) : '';
	
	//活动内容
	$actContent 	= isset($_POST['actContent']) ? addslashes($_POST['actContent']): '';
	
	//活动开始时间
	$begin_time 	= isset($_POST['begin_time']) ? strtotime($_POST['begin_time']) : 0;
	//活动领奖时间
	$award_time 	= isset($_POST['award_time']) ? strtotime($_POST['award_time']) : 0;
	//活动到期时间
	$end_time 		= isset($_POST['end_time']) ? strtotime($_POST['end_time']) : 0;
	
	//活动详细设置
	$activitys_type 				= isset($_POST['activitys_type']) ? $_POST['activitys_type'] : '';
	
	//执行脚本(lua函数名)
	$luaName 						= isset($_POST['luaName']) ? $_POST['luaName'] : '';
	
	//增加掉落物品(copyid=0表示普通地图掉落,mapid=0表示该类型怪掉落，mapmonsterid=0表示所有怪掉落)
	$copys_selected				= isset($_POST['copys_selected']) ? $_POST['copys_selected'] : '';
	$maps_selected 				= isset($_POST['maps_selected']) ? $_POST['maps_selected'] : '';
	$monsters_selected 		= isset($_POST['monsters_selected']) ? $_POST['monsters_selected'] : '';
	$items_selected				= isset($_POST['items_selected']) ? $_POST['items_selected'] : '';
	$items_min_num			= isset($_POST['items_min_num']) ? $_POST['items_min_num'] : '';
	$items_max_num			= isset($_POST['items_max_num']) ? $_POST['items_max_num'] : '';
	
	//删除商店商品(shopid:goodsid:index)
	$del_shops_selected		= isset($_POST['del_shops_selected']) ? $_POST['del_shops_selected'] : '';
	$del_goods_selected		= isset($_POST['del_goods_selected']) ? $_POST['del_goods_selected'] : '';
	$del_goods_index			= isset($_POST['del_goods_index']) ? $_POST['del_goods_index'] : '';
	
	//增加商店商品(shopid:goodsid:index)
	$add_shops_selected		= isset($_POST['add_shops_selected']) ? $_POST['add_shops_selected'] : '';
	$add_goods_selected		= isset($_POST['add_goods_selected']) ? $_POST['add_goods_selected'] : '';
	$add_goods_index			= isset($_POST['add_goods_index']) ? $_POST['add_goods_index'] : '';
	$interval 							= isset($_POST['interval']) ? $_POST['interval'] : '';
	
	//动态广播信息(inteval:info，inteval表示时间间隔，info表示广播内容)
	$info 								= isset($_POST['info']) ? $_POST['info'] : '';
	
	//右上角显示图标信息(图标编号)
	$iconId 							= 1025;
	
	
	//活动类型和活动规则组装字符串
	$actString = '';
	
	//活动类型为空则跳过
	if(!empty($activitys_type))
	{
		foreach($activitys_type as $key=>$val)
		{
			switch ($key)
			{
				case 1:			//lua脚本
					$actString .= '1:'.$luaName.'&'; 
					break;
				case 2:			//增加掉落物品
					foreach($copys_selected as $k=>$v)
					{
						$actString .= '2:';
						$actString .= $v.':'.$maps_selected[$k].':'.$monsters_selected[$k].':'.$items_selected[$k].':'.$items_min_num[$k].'-'.$items_max_num[$k].';&';
					}
					break;
				case 3:			//删除商店商品
					foreach($del_shops_selected as $k=>$v)
					{
						$actString .= '3:';
						$actString .= $v.':'.$del_goods_selected[$k].':'.$del_goods_index[$k].'&';
					}
					break;
				case 4:			//增加商店商品
					foreach($add_shops_selected as $k=>$v)
					{
						$actString .= '4:';
						$actString .= $v.':'.$add_goods_selected[$k].':'.$add_goods_index[$k].'&';
					}
					break;
				case 5:		//动态广播信息
						$actString .= '5:'.$interval.':'.$info.'&';
					break;
				case 6:		//右上角显示图标信息
						$actString .= '6:'.'1025';
					break;
			}
		}
	}
	
	$gifts = isset($_POST['gifts']) ? $_POST['gifts'] : null;
	if(!empty($gifts))
	{
		foreach($gifts as $k=>$v)
		{
			if(empty($v['item'])){
				exit('-1');			//制定礼包领取规则前必须设定礼包物品
			}
		}
	}
	
	if($act == 'save' && !empty($act_id))
	{
		//先保存在央服数据库
		$sendDataInfo = array('id'=>$act_id,'platform_id'=>addslashes(serialize($platformId)),'server_id'=>addslashes(serialize($severId)),
												'action'=>$action,'act_name'=>$actName,'act_content'=>$actContent,'act_interval'=>0,
												'start_date'=>date('Y-m-d H:i:s',$begin_time),'award_date'=>date('Y-m-d H:i:s',$award_time),'finish_date'=>date('Y-m-d H:i:s',$end_time),
												'act_info'=>$actString,'actgift_info'=>addslashes(serialize(($gifts))));
		$sql = makeSql($sendDataInfo,0,'tb_ad_activity','replace');		//修改 
	}
	else 
	{
		//先保存在央服数据库
		$sendDataInfo = array('platform_id'=>addslashes(serialize($platformId)),'server_id'=>addslashes(serialize($severId)),
												'action'=>$action,'act_name'=>$actName,'act_content'=>$actContent,'act_interval'=>0,
												'start_date'=>date('Y-m-d H:i:s',$begin_time),'award_date'=>date('Y-m-d H:i:s',$award_time),'finish_date'=>date('Y-m-d H:i:s',$end_time),
												'act_info'=>$actString,'actgift_info'=>addslashes(serialize($gifts)));
		$sql = makeSql($sendDataInfo,0,'tb_ad_activity');		//新建
	}
	
	$isSave = $db->query($sql);
	$isSave ? exit('1'):exit('0');
	
}
elseif($act == 'modify')			//本地编辑
{
	$act_id = isset($_GET['actId'])?$_GET['actId']:NULL;
	$sql = "SELECT platform_id,server_id,action,id as act_id,act_name,act_content,`act_interval`,start_date,award_date,finish_date,act_info,actgift_info 
				FROM tb_ad_activity WHERE id= $act_id";
	$activity = $db->getRow($sql);
	
	//解析平台名
	$activity['platform_id'] = unserialize($activity['platform_id']);
	//解析服务器名
	$activity['server_id'] = unserialize($activity['server_id']);
	
	//解析活动规则信息
	//1:lua&2:0:4:367:3:2-44;&3:2:329:3&4:5:11000:7&5:1:动态广播信息&6:5
	$act_info = explode('&',$activity['act_info']);
	$items_edit =$del_goods=$add_goods= array();
	foreach($act_info as  $k=>$v)
	{
		$temp = explode(':', $v);
		switch($temp[0])
		{
			case 1:
				$luaName=$temp[1];
				break;
			case 2:
				$items_edit[$k]['copyid'] = $temp[1];
				$items_edit[$k]['mapid'] = $temp[2];
				$items_edit[$k]['mapmonsterid'] = $temp[3];
				$items_edit[$k]['item'] = $temp[4];
				$tmp = explode('-',$temp[5]);
				$items_edit[$k]['item_min_num'] = $tmp[0];
				$items_edit[$k]['item_max_num'] = rtrim($tmp[1],';');
				break;
			case 3:
				$del_goods[$k]['shopid'] = $temp[1];
				$del_goods[$k]['goodsid'] = $temp[2];
				$del_goods[$k]['index'] = $temp[3];
				break;
			case 4:
				$add_goods[$k]['shopid'] = $temp[1];
				$add_goods[$k]['goodsid'] = $temp[2];
				$add_goods[$k]['index'] = $temp[3];
				break;
			case 5:
				$interval =$temp[1];
				$info= $temp[2];
				break;
			case 6:
				$iconId = $temp[1];
				break;
		}
	}
	
	if(!empty($items_edit))
	{
		$maps_edit = array();
		foreach($items_edit as $k=>$v)
		{
				$copyId = $v['copyid'];
				
				if($copyId == 0)			//普通地图 
				{
					$sql = "SELECT id as map_id,name  FROM tb_table_map WHERE is_copy= 0;";
				}
				else 							//副本地图
				{
					$sql = "SELECT ml.map_id,m.`name`
							FROM tb_table_mapcopy_list AS ml
							LEFT JOIN tb_table_map AS m ON m.id = ml.map_id
							WHERE mapcopy_id = {$copyId}
							GROUP BY ml.map_id";
				}
				
				$maps_edit[$k] = $db->getAll($sql);
		}
		$T->assign('maps_edit',$maps_edit);
		
		$monsters_edit = array();
		foreach($items_edit as $k=>$v)
		{
				$mapId = $v['mapid'];
				
				$sql = "SELECT mm.id,mi.name 
							FROM tb_table_mapmonster AS mm
							LEFT JOIN tb_table_monsterinfo AS mi ON mi.id = mm.monster_id
							GROUP BY mm.monster_id";
				
				$monsters_edit[$k] = $db->getAll($sql);
		}
		$T->assign('monsters_edit',$monsters_edit);
	}
	
	if(!empty($del_goods))
	{
		$del_goods_edit = array();
		foreach($del_goods as $k=>$v)
		{
			$sql = "SELECT s.id,g.`name` FROM tb_table_shopgoods as s
						LEFT JOIN tb_table_goods AS g ON g.id = s.goods_id
						WHERE s.shop_id =  {$v['shopid']}";
			$del_goods_edit[$k] = $db->getAll($sql);
		}
		$T->assign('del_goods_edit',$del_goods_edit);
	}
	
	if(!empty($add_goods))
	{
		$add_goods_edit = array();
		foreach($add_goods as $k=>$v)
		{
			$sql = "SELECT s.id,g.`name` FROM tb_table_shopgoods as s
						LEFT JOIN tb_table_goods AS g ON g.id = s.goods_id
						WHERE s.shop_id =  {$v['shopid']}";
			$add_goods_edit[$k] = $db->getAll($sql);
		}
		$T->assign('add_goods_edit',$add_goods_edit);
	}
	
	//解析活动奖励信息
	$temp = $activity['actgift_info'];
	$gifts = unserialize(stripslashes($temp));
	
	
	$T->assign('act_id',$act_id);
	$T->assign('act',$act);
	$T->assign('server_list_one',$activity['server_id']);
	$T->assign('actName',$activity['act_name']);
	$T->assign('start_date',$activity['start_date']);
	$T->assign('award_date',$activity['award_date']);
	$T->assign('finish_date',$activity['finish_date']);
	$T->assign('act_content',stripslashes($activity['act_content']));
	
	$T->assign('luaName',$luaName);
	$T->assign('items_edit',$items_edit);
	$T->assign('del_goods',$del_goods);
	$T->assign('add_goods',$add_goods);
	$T->assign('interval',$interval);
	$T->assign('info',$info);
	$T->assign('iconId',$iconId);
	
	$T->assign('gifts',$gifts);
	$T->assign('activity',$activity);
}
elseif($act == 'map')				//某个副本下的地图列表
{
	$maps = array();
	$copyId = isset($_GET['copyid']) ? $_GET['copyid'] : 1;
	
	if($copyId == 0)			//普通地图 
	{
		$sql = "SELECT id as map_id,name  FROM tb_table_map WHERE is_copy= 0;";
	}
	else 							//副本地图
	{
		$sql = "SELECT ml.map_id,m.`name`
				FROM tb_table_mapcopy_list AS ml
				LEFT JOIN tb_table_map AS m ON m.id = ml.map_id
				WHERE mapcopy_id = {$copyId}
				GROUP BY ml.map_id";
	}
	
	$maps = $db->getAll($sql);
	
	echo json_encode($maps);	
	exit;
}
elseif($act == 'mapmonsters')		//某个地图下的怪物列表
{
	$mapmonsters = array();
	$mapid = isset($_GET['mapid']) ? $_GET['mapid'] : 1;
	$sql = "SELECT mm.id,mi.name 
				FROM tb_table_mapmonster AS mm
				LEFT JOIN tb_table_monsterinfo AS mi ON mi.id = mm.monster_id
				WHERE mm.map_id = $mapid AND mm.monster_id>32768";
	$mapmonsters = $db->getAll($sql);
	echo json_encode($mapmonsters);	
	exit;
}
elseif($act == 'goodslist')			//某个商店中的商品列表
{
	$goodslist = array();
	$shopid = isset($_GET['shopid']) ? $_GET['shopid'] : 1;
	$sql = "SELECT s.id,g.`name` FROM tb_table_shopgoods as s
				LEFT JOIN tb_table_goods AS g ON g.id = s.goods_id
				WHERE s.shop_id =  $shopid";
	$goodslist = $db->getAll($sql);
	
	echo json_encode($goodslist);	
	exit;
}
elseif($act == 'getItemId')					//ajax根据道具名获取道具ID
{
	$giftItemName = trim($_GET['giftItemName']);
	$sql = "SELECT id FROM tb_table_item WHERE item_name = '{$giftItemName}'";
	$id = $db->getOne($sql);
	echo $id ? json_encode($id): 0;
	exit; 
}	
elseif($act == 'getItemName')			//道具名
{			
	$giftItemId = trim($_GET['giftItemId']);
	$sql="select item_name from tb_table_item where id = $giftItemId";
	$name = $db->getOne($sql);
	echo $name ? json_encode($name) : 0;
	exit;
}

//平台列表
$platforms = getPlatforms();

//服务器列表
$server_list = getServerInfo();

//副本列表
$copys = array();
$sql = "SELECT id,copy_name FROM tb_table_mapcopy";
$copys = $db->getAll($sql);

//地图列表
$maps = array();
$sql = "SELECT id as map_id,name FROM tb_table_map;";
$maps = $db->getAll($sql);

//怪物列表
$monsters = array();
$sql = "SELECT mm.id,mi.name 
			FROM tb_table_mapmonster AS mm
			LEFT JOIN tb_table_monsterinfo AS mi ON mi.id = mm.monster_id
			GROUP BY mm.monster_id";
$monsters = $db->getAll($sql);

//物品列表
$itemlibs = array();
$sql = "SELECT id,lib_name  FROM tb_table_item_lib WHERE lib_name != '0'";
$itemlibs = $db->getAll($sql);
$T->assign('itemlibs',$itemlibs);	

//商店列表
$shops = array();
$sql = "SELECT id,name FROM tb_table_shop";
$shops = $db->getAll($sql);

//排行榜类型
$rankTypes = array(
	11=>'等级排行榜',
	12=>'战斗力排行榜',
	13=>'武器排行榜',
	14=>'宠物排行榜',
	15=>'宠物技能排行榜',
	16=>'功勋排行榜',
	21=>'充值排行榜',
 	22=>'铜币排行版',
	31=>'成就排行版',
	32=>'崇拜排行版',
	33=>'鄙视排行版',
	34=>'魅力排行版',
	35=>'博文排行版',
	41=>'帮派繁荣排行版',
	42=>'派实力排行版',
	43=>'国家繁荣排行版',
	44=>'国家实力排行版',
	45=>'砖块排行版',
	51=>'八面森林简单排行版',
	52=>'八面森里困难排行版',
	53=>'八面森林精英排行版',
	54=>'迷雾之巅简单排行版',
	55=>'迷雾之巅困难排行版',
	56=>'迷雾之巅精英排行版',
	57=>'环境试炼排行版',
	58=>'天道排行版',
	59=>'地灵排行版',
	60=>'人王排行版',
);

//系统功能列表
$sysFunctions = array();
$sql = "SELECT param_name,param_value FROM tb_system_parameter WHERE param_type = 0x41";
$sysFunctions = $db->getAll($sql);

//礼包卡类型
$sql = "SELECT id,act_type FROM tb_actcode_type";
$actTypes = $db->getAll($sql);

$T->assign('copys',$copys);	
$T->assign('maps',$maps);
$T->assign('monsters',$monsters);
$T->assign('shops',$shops);
$T->assign('rankTypes',$rankTypes);
$T->assign('sysFunctions',$sysFunctions);
$T->assign('actTypes',$actTypes);
$T->assign('platforms',$platforms);
$T->assign('server_list',$server_list);
$T->display('activity_manage.html');

